package com.mud.mudcommune.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.SubjectEntry;
import com.mud.mudcommune.view.fragment.threeLevel.WebViewFragment;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/19 0019.
 */

public class SubjectAdapter extends MyBaseAdapter {
    private Context context;
    private List list;
    private static Fragment fragment;
    private static FragmentManager manager;
    private static SubjectEntry.Subject subject;

    public SubjectAdapter(Context context, List list, Fragment fragment, FragmentManager manager) {
        super(list);
        this.context = context;
        this.list = list;
        this.fragment = fragment;
        this.manager = manager;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        subject = (SubjectEntry.Subject) list.get(position);
        SubjectAdapter.ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.subject_item, null);
            viewHolder = new SubjectAdapter.ViewHolder(view);
        } else {
            viewHolder = (SubjectAdapter.ViewHolder) view.getTag();
        }
        if (subject != null) {
            Glide.with(context)
                    .load(subject.getTopic_img())
                    .into(viewHolder.backgound);
        }
        viewHolder.title.setText(subject.getTitle());
        return view;
    }

    static class ViewHolder implements View.OnClickListener {
        ImageView backgound;
        TextView title;
        ImageView icon;

        public ViewHolder(View view) {
            backgound = ButterKnife.findById(view, R.id.Special_back);
            title = ButterKnife.findById(view, R.id.Special_textview);
            backgound.setOnClickListener(this);
            view.setTag(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.Special_back:
                    Fragment webview = WebViewFragment.getInstance();
                    Bundle bundle = new Bundle();
                    bundle.putString("Subject", subject.getTopic_url());
                    webview.setArguments(bundle);
                    manager.beginTransaction()
                            .hide(fragment)
                            .add(R.id.main_frame, WebViewFragment.getInstance())
                            .addToBackStack(null)
                            .commit();
                    break;
            }
        }
    }
}
